package com.sysu.simulation.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sysu.simulation.BaseFragment;
import com.sysu.simulation.R;
import com.sysu.simulation.bean.Clothes;
import com.sysu.simulation.helper.AccountHelper;
import com.sysu.simulation.helper.ApiHelper;

import cz.msebera.android.httpclient.Header;

public class EditClothesFragment extends BaseFragment implements View.OnClickListener{

    private OnClothesEditedListener mListener;
    private Spinner sMaterial, sThickness, sType;
    private EditText mClothesName;
    private Button submit_button;
    private Clothes mClothes;
    private int mPos;
    private String oldName;

    public EditClothesFragment() {

    }

    public static EditClothesFragment newInstance() {
        EditClothesFragment fragment = new EditClothesFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnClothesEditedListener) {
            mListener = (OnClothesEditedListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnClothesEditedListener");
        }
    }

    @Override
    protected void initWidget(View root) {
        sMaterial = (Spinner) root.findViewById(R.id.spinner_material);
        sThickness = (Spinner) root.findViewById(R.id.spinner_thickness);
        sType = (Spinner) root.findViewById(R.id.spinner_type);

        mClothesName = (EditText) root.findViewById(R.id.edit_clothesName);
        submit_button = (Button) root.findViewById(R.id.submit_add_clothes);
        submit_button.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        ArrayAdapter<Clothes.Material> materialAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Material.values());
        ArrayAdapter<Clothes.Thickness> thicknessAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Thickness.values());
        ArrayAdapter<Clothes.Type> typeAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, Clothes.Type.values());

        sMaterial.setAdapter(materialAdapter);
        sThickness.setAdapter(thicknessAdapter);
        sType.setAdapter(typeAdapter);

        submit_button.setText("更新服装");
        mClothesName.setEnabled(false);
        mClothesName.setFocusable(false);
        setDefaultInput(getArguments());
    }

    public void setDefaultInput(Bundle bundle) {
        mClothes = new Gson().fromJson(bundle.getString("clothes_item"), Clothes.class);
        mPos = bundle.getInt("clothes_position");
        oldName = mClothes.getTitle();

        mClothesName.setText(mClothes.getTitle());
        sMaterial.setSelection(mClothes.getMaterial().ordinal());
        sThickness.setSelection(mClothes.getThickness().ordinal());
        sType.setSelection(mClothes.getType().ordinal());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View view) {
        if(checkInput()) {
            mClothes.setMaterial((Clothes.Material)sMaterial.getSelectedItem());
            mClothes.setThickness((Clothes.Thickness)sThickness.getSelectedItem());
            mClothes.setType((Clothes.Type)sType.getSelectedItem());
            doSubmit();
        }
    }

    private boolean checkInput() {
        boolean isValid = true;
        if(!oldName.equals(mClothesName.getText().toString().trim())) {
            Toast.makeText(mContext,"不能更改服装名称", Toast.LENGTH_SHORT).show();
            isValid = false;
        }
        return isValid;
    }

    private void doSubmit() {
        String uid = AccountHelper.getUserId();
        String jsonClothes = new Gson().toJson(mClothes);
        ApiHelper.editUserClothes(uid, jsonClothes, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(mContext,"网络请求错误，请重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if(responseString.equals("-1")) {
                    Toast.makeText(mContext,"提交数据不完整，请重试", Toast.LENGTH_SHORT).show();
                }
                else if(responseString.equals("-2")) {
                    Toast.makeText(mContext,"服装不存在，请先创建", Toast.LENGTH_SHORT).show();
                }
                else {
                    mListener.onClothesEdited(mClothes, mPos);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_clothes;
    }

    public interface OnClothesEditedListener {
        void onClothesEdited(Clothes item, int position);
    }
}
